import static spark.Spark.*;
import spark.Filter;
import spark.Request;
import spark.Response;

public class Filters {
	//
	private boolean isPublicRequest(Request request) {
		String path = request.servletPath();

		switch(path) {// should be with regular expression
			case "/public/css/main.css":return true;
			default: return false;
		}
	}
	
	public void init(){

		 before(new Filter("/login") {
			 
			@Override
			public void handle(Request request, Response response) {
				if(request.session().attribute("user") != null) {
					
					response.redirect(request.contextPath());
				} 
			}
		 });
		
		 before(new Filter() {
			@Override
			public void handle(Request request, Response response) {
				if(isPublicRequest(request)){
					
				} else if(!request.servletPath().equals("/login") 
						&& request.session().attribute("user") == null){
					response.redirect(request.contextPath() + "/login");
				}
			}
		 });
		 
	}
}